from django.db import models
from django.contrib.auth import get_user_model
from django.db.models.signals import post_save
from django.dispatch import receiver
# Create your models here.
User = get_user_model()
class CaptchaIdentify(models.Model):
    objects = models.Manager()
    email = models.EmailField(unique=True)
    captcha = models.CharField(max_length=4)
    created_at = models.DateTimeField(auto_now_add=True)


#补充信息
def save_path(instance, filename):
    ext = filename.split('.')[-1]
    filename = 'icon/{}.{}'.format(instance.user.username, ext)
    return filename

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

class UserProfile(models.Model):
    objects = models.Manager()
    user = models.OneToOneField(User, on_delete=models.CASCADE,related_name='profile')
    icon = models.ImageField(upload_to=save_path,default="static/img/headinstance.jpg" ,null=True, blank=True)